<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class DefaultNotification extends Notification
{
    use Queueable;

    public $viaTypes;  //通道类型
    public $data;      //通知内容

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($data, $config = [])
    {
        $this->data = $data;
        foreach ($config as $name => $value) {
            $this->$name = $value;
        }
        //dd($this->viaTypes);
    }

    /**
     * 指定发送通道
     * Get the notification's delivery channels.
     * mail       发送邮件
     * database   数据库
     * broadcast  广播
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return  $this->viaTypes  ?  (array)$this->viaTypes : ['database'];
    }

    /**
     * 发送数据库通知
     * @param $notifiable
     * @author jiangyong<jiangyong.go@qq.com>
     * @date   2025/5/26 $END
     */
    public function toDatabase($notifiable)
    {
        return $this->data ?? [];
    }

    public function toBroadcast($notifiable)
    {
        //return (new BroadcastMessage($this->data));
        return (new BroadcastMessage($this->data))->onQueue('{broadcast}');
    }

    /**
     * Get the mail representation of the notification.
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return $notifiable;
    }
}
